package com.example.lean;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    SensorManager sm;   //用来引用传感器管理员
    Sensor sr;   //用来引用加速传感器对象
    TextView txv;    //用来引用画面中的文字组件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sm = (SensorManager) getSystemService(SENSOR_SERVICE);    //从系统服务获取传感器管理员
        sr = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    //获取加速传感器
        txv = findViewById(R.id.textView);    //获取TextView组件
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        //加速度改变时
        String msg = String.format("X轴:%1.2f\nY轴:%1.2f\nZ轴:%1.2f", event.values[0], event.values[1], event.values[2]);
        txv.setText(msg);
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
    }

    @Override
    protected void onResume() {
        super.onResume();
        //当Activity界面显示出来时
        sm.registerListener(this, sr, SensorManager.SENSOR_DELAY_NORMAL);     //向加速传感器（sr）注册监听对象(this)
    }

    @Override
    protected void onPause() {
        super.onPause();
        //取消监听对象（this)的注册
        sm.unregisterListener(this);
    }

}

